package com.baijiayun.live.ui.base

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import android.os.Bundle
import com.baijiayun.live.ui.activity.LiveRoomRouterListener
import com.baijiayun.live.ui.isAdmin
import com.baijiayun.live.ui.pptpanel.MyPadPPTView
import com.baijiayun.live.ui.speakerlist.item.RemoteItem
import com.baijiayun.live.ui.speakerlist.item.Switchable
import com.baijiayun.live.ui.toolbox.questionanswer.QADetailFragment
import com.baijiayun.livecore.context.LPConstants
import com.baijiayun.liveuibase.toolbox.rollcall.RollCallStatus
import com.baijiayun.livecore.context.LPError
import com.baijiayun.livecore.context.LiveRoom
import com.baijiayun.livecore.listener.OnPhoneRollCallListener
import com.baijiayun.livecore.models.*
import com.baijiayun.livecore.models.broadcast.LPMainScreenNoticeModel
import com.baijiayun.livecore.models.imodels.IMediaModel
import com.baijiayun.livecore.models.imodels.IUserModel
import com.baijiayun.livecore.models.launch.LPEnterRoomNative
import com.baijiayun.liveuibase.base.BaseUIConstant
import com.baijiayun.liveuibase.toolbox.responder.model.ResponderRecordModel
import com.baijiayun.liveuibase.widgets.toolbar.BaseGraphMenuWindow
import com.baijiayun.liveuibase.widgets.toolbar.ColorSelectData
import com.baijiayun.liveuibase.widgets.toolbar.ShapeChangeData
import com.baijiayun.liveuibase.widgets.toolbar.WidthSelectData

/**
 * activity与fragment、fragment之间的通信，不包含业务逻辑
 * Created by Shubo on 2019-10-10.
 */
class RouterViewModel : ViewModel() {
    enum class QuizStatus {
        NOT_INIT, START, RES, END, SOLUTION, CLOSE
    }

    lateinit var liveRoom: LiveRoom

    fun isLiveRoomInitialized() = ::liveRoom.isInitialized

    var enterRoomSuccess = false

    var isReConnect = false

    val marqueeAsynchronousDisplay = MutableLiveData<Pair<Boolean, LPEnterRoomNative.LPHorseLamp>>()

    val action2PPTError = MutableLiveData<Pair<Int, String?>>()

    val showEvaDlg = MutableLiveData<Boolean>()

    val quizStatus = MutableLiveData<Pair<QuizStatus, LPJsonModel>>()

    /**
     * 计时器操作
     */
    val showTimer = MutableLiveData<Pair<Boolean, LPBJTimerModel>>()

    val showTimerShowy = MutableLiveData<Pair<Boolean, LPBJTimerModel>>()

    val answerStart = MutableLiveData<LPAnswerModel>()

    val answerEnd = MutableLiveData<LPComponentDestroyModel>()

    val redPacketPublish = MutableLiveData<Unit>()

    /**
     * 开关音视频
     */
    val notifyLocalPlayableChanged = MutableLiveData<Pair<Boolean, Boolean>>()

    /**
     * 开关视频
     */
    val notifyLocalVideoChanged = MutableLiveData<Boolean>()

    val notifyRemotePlayableChanged = MutableLiveData<IMediaModel>()

    val notifyMixModePresenterChange = MutableLiveData<LPUserModel>()

    val actionAttachLocalVideo = MutableLiveData<Boolean>()

    val actionAttachLocalAVideo = MutableLiveData<Boolean>()

    val actionAttachLocalAudio = MutableLiveData<Boolean>()

    val notifyCloseRemoteVideo = MutableLiveData<RemoteItem>()

    val actionExit = MutableLiveData<Unit>()

    var actionNavigateToMain = MutableLiveData<Boolean>()

    val actionShowQuickSwitchPPT = MutableLiveData<Bundle>()

    val actionChangePPT2Page = MutableLiveData<Int>()

    val notifyPPTPageCurrent = MutableLiveData<Int>()

    val addPPTWhiteboardPage = MutableLiveData<Unit>()

    val deletePPTWhiteboardPage = MutableLiveData<Int>()

    val changePPTPage = MutableLiveData<Pair<String?, Int>>()

    //分享操作
    val action2Share = MutableLiveData<Unit>()

    val isShowShare = MutableLiveData<Boolean>()

    val isShowEyeCare = MutableLiveData<Boolean>()

    val action2Setting = MutableLiveData<Unit>()

    /**
     * 出错的记录值 用于保存直播间相关错误状态 做对应的 UI 修改
     */
    val actionShowError = MutableLiveData<LPError>()

    val actionReEnterRoom = MutableLiveData<Pair<Boolean, Boolean>>()

    val actionDismissError = MutableLiveData<Unit>()

    val actionRequestActiveUsers = MutableLiveData<Unit>()

    val handsupList = MutableLiveData<List<IUserModel>>()

    val actionShowPPTManager = MutableLiveData<Boolean>()

    //切到全屏
    val switch2FullScreen = MutableLiveData<Pair<Boolean, Switchable?>>()

    //切到ppt区域
    val switch2MaxScreen = MutableLiveData<Switchable>()

    val switch2SpeakList = MutableLiveData<Switchable>()

    val switch2MainVideo = MutableLiveData<Switchable>()

    val removeMainVideo = MutableLiveData<Switchable>()

    var mainVideoItem: Switchable? = null

    var registerSyncPPTVideo = MutableLiveData<Boolean>()

    val pptViewData = MutableLiveData<MyPadPPTView>()

    //辅助摄像头，默认全屏，切换位置当做ppt处理
    val extCameraData = MutableLiveData<Pair<String, Switchable>>()

    //发言状态
    val speakApplyStatus = MutableLiveData<Int>()

    // region 画笔相关
    // 画笔模式切换
    val actionNavigateToPPTDrawing = MutableLiveData<ShapeChangeData>()

    // 画笔颜色变更
    val drawColorChange = MutableLiveData<ColorSelectData>()

    // 画笔宽度变更
    val drawWidthChange = MutableLiveData<WidthSelectData>()

    // 自定义图形变更
    val drawGraphChange = MutableLiveData<BaseGraphMenuWindow.OnShapeChangeModel>()

    // 文字画笔字号变更
    val drawTextSizeChange = MutableLiveData<Int>()
    // 编辑文字画笔
    val editTextShape = MutableLiveData<String>()
    // endregion

    //课程是否开始
    val isClassStarted = MutableLiveData<Boolean>()

    val classEnd = MutableLiveData<Unit>()

    val action2RedPacketUI = MutableLiveData<Pair<Boolean, LPRedPacketModel?>>()

    val sendPictureMessage = MutableLiveData<String>()

    val showSavePicDialog = MutableLiveData<ByteArray?>()

    val saveChatPictureToGallery = MutableLiveData<ByteArray?>()

    val speakListCount = MutableLiveData<Int>()

    val notifyAward = MutableLiveData<LPInteractionAwardModel>()

    val action2Award = MutableLiveData<Pair<String,LPInteractionAwardModel>>()

    val awardRecord = HashMap<String, LPAwardUserInfo>()

    val showTeacherIn = MutableLiveData<Boolean>()

    val showPresenterIn = MutableLiveData<Unit>()

    val actionShowSendMessageFragment = MutableLiveData<String>()

    val actionShowAnnouncementFragment = MutableLiveData<Boolean>()

    val actionShowQAInteractiveFragment = MutableLiveData<Unit>()

    // 关闭画笔模式 : All -> Normal
    val closeDrawingMode = MutableLiveData<Unit>()

    val shouldShowTecSupport = MutableLiveData<Boolean>()

    val action2Chat = MutableLiveData<Boolean>()

    val action2ChatBottom = MutableLiveData<Boolean>()

    val privateChatUser = MutableLiveData<IUserModel>()

    val timeOutStart = MutableLiveData<Pair<String, Boolean>>()

    val kickOut = MutableLiveData<Unit>()

    var checkUnique = true

    var chatLabelVisiable = false

    var choosePrivateChatUser = false

    var forbidChatUserNums = HashSet<String>()

    var invitingUserIds = HashSet<String>()

    var remarkEnable = MutableLiveData<Boolean>()

    val hasNewQa = MutableLiveData<Boolean>()

    val hasNewQaPublished = MutableLiveData<Boolean>()

    //QA页面是否打开着
    var isQaOpen = false

    val actionShowWebpage = MutableLiveData<String>()

    val actionCloseWebpage = MutableLiveData<String>()

    val notifyPresenterChange = MutableLiveData<Pair<String, IMediaModel>>()

    //首次获取主讲人
    val actionPresenterIn = MutableLiveData<String>()

    val actionChooseFiles = MutableLiveData<BaseUIConstant.UploadType>()

    val actionShowToast = MutableLiveData<String>()

    val action2Lottery = MutableLiveData<LPLotteryResultModel>()

    val actionCommandLotteryStart = MutableLiveData<LPCommandLotteryModel>()

    val actionDismissLottery = MutableLiveData<Unit>()

    val action2MyQAList = MutableLiveData<Unit>()

    val action2SaveQuestion = MutableLiveData<Pair<QADetailFragment.QATabStatus, Map<String, String>>>()

    val action2UpdateTheme = MutableLiveData<Unit>()

    var rollCallStatus = RollCallStatus.None

    var responderRecord = ArrayList<ResponderRecordModel>()

    val showRollCall = MutableLiveData<Pair<Int, OnPhoneRollCallListener.RollCall?>>()

    val dismissRollCall = MutableLiveData<Boolean>()

    val showResponder = MutableLiveData<Boolean>()

    val responderStart = MutableLiveData<LPAnswerRacerStartModel>()

    val responderEnd = MutableLiveData<LPAnswerRacerEndModel>()

    var actionRollCallResult = MutableLiveData<LPRoomRollCallResultModel>()

    val startScreenShare = MutableLiveData<Unit>()

    fun canOperateH5PPT() = isLiveRoomInitialized() && liveRoom.partnerConfig.enableUploadZipH5PPT == 1 && isAdmin(liveRoom)
    fun exitFullScreen() {
        if (switch2FullScreen.value?.first == true) {
            switch2FullScreen.value?.second?.switchToFullScreen(false)
        }
    }

    //主屏公告
    val action2ShowMainScreenNotice = MutableLiveData<List<LPMainScreenNoticeModel>>()

    val actionRoomLayoutSwitch = MutableLiveData<Boolean>()

    var routerListener: LiveRoomRouterListener? = null
    //是否是视频墙,纯视频
    fun isLiveWall() :Boolean{
        if (!isLiveRoomInitialized()) {
            return false
        }
        return liveRoom.templateType == LPConstants.TemplateType.VIDEO
                || (liveRoom.templateType == LPConstants.TemplateType.LIVE_WALL
                && liveRoom.currentRoomLayout == LPConstants.RoomLayoutMode.GALLERY)

    }

    fun canAward(user: IUserModel?): Boolean {
        if (!isLiveRoomInitialized()) {
            return false
        }
        if (!liveRoom.isClassStarted) {
            return false
        }
        if (user == null) {
            return false
        }
        if (user.type != LPConstants.LPUserType.Student) {
            return false
        }
        if (liveRoom.isGroupClass) {
            if (liveRoom.isTeacherOrAssistant) {
                return true
            } else if (liveRoom.isGroupTeacherOrAssistant) {
                return liveRoom.currentUser.group == user.group
            }
            return false
        } else {
            return liveRoom.isTeacherOrAssistant
        }
    }
    val actionShowBleDialog = MutableLiveData<Unit>()
    val actionShowBonusPointsWindow = MutableLiveData<Boolean>()
    val screenShareEndBackstage = MutableLiveData<Unit>()
}